﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Web.Framework;
using System.Data;
using Portal.AssetTn.Model;
using System.IO;

namespace Portal.Gui.Web.WebParts.AssetTn
{
    public class AssetViewField
    {
        public string Id;
        public string Title;
        public string ToolTips;
        public string SqlField;
        public int LookUp=0;
        public string LookUpTable;
        public string LookUpKeyId;
        public int Key = 0;
        public string FieldType;
        public string SortIndex;
        public int Active = 1;

        public AssetViewField()
        {
        }
    }



    public class AssetViewFieldParser
    {
        public static AssetViewField CreateInstance()
        {
            //tao 1 class trang....
            AssetViewField oAssetViewField = new AssetViewField();
            return oAssetViewField;
        }

        public static AssetViewField ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            AssetViewField oAssetViewField = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetViewField;
        }


        public static AssetViewField ParseFromDataRow(DataRow dr)
        {
            AssetViewField oAssetViewField = new AssetViewField();

            if (dr["Active"] != DBNull.Value)
            {
                oAssetViewField.Active = int.Parse(dr["Active"].ToString());
            }
            if (dr["FieldType"] != DBNull.Value)
            {
                oAssetViewField.FieldType = (string)dr["FieldType"];
            }
            if (dr["Id"] != DBNull.Value)
            {
                oAssetViewField.Id = (string)dr["Id"];
            }
            if (dr["Key"] != DBNull.Value)
            {
                oAssetViewField.Key = int.Parse(dr["Key"].ToString());
            }
            if (dr["LookUp"] != DBNull.Value)
            {
                oAssetViewField.LookUp = int.Parse(dr["LookUp"].ToString());
            }
            if (dr["LookUpKeyId"] != DBNull.Value)
            {
                oAssetViewField.LookUpKeyId = (string)dr["LookUpKeyId"];
            }
            if (dr["LookUpTable"] != DBNull.Value)
            {
                oAssetViewField.LookUpTable = (string)dr["LookUpTable"];
            }
            if (dr["SortIndex"] != DBNull.Value)
            {
                oAssetViewField.SortIndex = (string)dr["SortIndex"];
            }
            if (dr["SqlField"] != DBNull.Value)
            {
                oAssetViewField.SqlField = (string)dr["SqlField"];
            }
            if (dr["Title"] != DBNull.Value)
            {
                oAssetViewField.Title = (string)dr["Title"];
            }
            if (dr["ToolTips"] != DBNull.Value)
            {
                oAssetViewField.ToolTips = (string)dr["ToolTips"];
            }
            return oAssetViewField;
        }

        public static AssetViewField[] ParseFromDataTable(DataTable dtTable)
        {
            AssetViewField[] AssetViewField = new AssetViewField[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetViewField[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetViewField;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("AssetViewField");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("Id");
            ds.Tables["Info"].Columns.Add("Title");
            ds.Tables["Info"].Columns.Add("ToolTips");
            ds.Tables["Info"].Columns.Add("SqlField");
            ds.Tables["Info"].Columns.Add("LookUp");
            ds.Tables["Info"].Columns.Add("LookUpTable");
            ds.Tables["Info"].Columns.Add("LookUpKeyId");
            ds.Tables["Info"].Columns.Add("Key");
            ds.Tables["Info"].Columns.Add("SortIndex");
            ds.Tables["Info"].Columns.Add("Active");
            ds.Tables["Info"].Columns.Add("FieldType");
            ds.Tables["Info"].Columns.Add("DefaultValue");

            return ds;
        }

        public static string GetXml(AssetViewField oAssetViewField)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetViewField.Id,
                        oAssetViewField.Title,
                        oAssetViewField.ToolTips,
                        oAssetViewField.SqlField,
                        oAssetViewField.LookUp,
                        oAssetViewField.LookUpTable,
                        oAssetViewField.LookUpKeyId,
                        oAssetViewField.Key,
                        oAssetViewField.SortIndex,
                        oAssetViewField.Active,
                        oAssetViewField.FieldType,
                        ""
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetViewField oAssetViewField = ParseFromDataRow(dr);
            string Xml = GetXml(oAssetViewField);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetViewField[] AssetViewField = new AssetViewField[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetViewField[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetViewField);
            return Xml;
        }


        public static string GetXml(AssetViewField[] AssetViewField)
        {
            if (AssetViewField == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetViewField = new AssetViewField[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetViewField.Length; iIndex++)
            {
                AssetViewField oAssetViewField = AssetViewField[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                        oAssetViewField.Id,
                        oAssetViewField.Title,
                        oAssetViewField.ToolTips,
                        oAssetViewField.SqlField,
                        oAssetViewField.LookUp,
                        oAssetViewField.LookUpTable,
                        oAssetViewField.LookUpKeyId,
                        oAssetViewField.Key,
                        oAssetViewField.SortIndex,
                        oAssetViewField.Active,
                        oAssetViewField.FieldType,
                        "",
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetViewField[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetViewField[0];
            }
            AssetViewField[] AssetViewField = new AssetViewField[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetViewField[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetViewField;
        }
    }





    public class AssetViewFieldBll
    {
        public static System.Collections.ObjectModel.Collection<AssetViewField> 
            GetAssetViewFields(string SiteId)
        {
            SiteParam OSiteParam = Environments.CreateSiteParam(SiteId);
            string XmlFile = OSiteParam.PathRoot + "\\data\\xml.asset.view\\all-fields.xml";
            DataSet ds = new DataSet();
            if (System.IO.File.Exists(XmlFile) == false)
            {
                ds.Tables.Add("XmlInfo");
                ds.Tables["XmlInfo"].Columns.Add("Id");
                ds.Tables["XmlInfo"].Columns.Add("Title");
                ds.Tables["XmlInfo"].Columns.Add("ToolTips");
                ds.Tables["XmlInfo"].Columns.Add("SqlField");
                ds.Tables["XmlInfo"].Columns.Add("LookUp");
                ds.Tables["XmlInfo"].Columns.Add("LookUpTable");
                ds.Tables["XmlInfo"].Columns.Add("LookUpKeyId");
                ds.Tables["XmlInfo"].Columns.Add("Key");
                ds.Tables["XmlInfo"].Columns.Add("FieldType");
                ds.Tables["XmlInfo"].Columns.Add("SortIndex");
                ds.Tables["XmlInfo"].Columns.Add("Active");
                ds.Tables["XmlInfo"].Rows.Add(new object[]{
                    "Id", "Title", "ToolTips", "SqlField", "LookUp", "LookUpTable", "LookUpKeyId", "Key", "FieldType", "SortIndex","Active"
                });
                ds.WriteXml(XmlFile);
            }
            else
            {
                ds.ReadXml(XmlFile);
            }

            System.Collections.ObjectModel.Collection<AssetViewField> 
                mAssetViewFields = new System.Collections.ObjectModel.Collection<AssetViewField> { };
            for (int iIndex = 0; iIndex < ds.Tables["XmlInfo"].Rows.Count; iIndex++)
            {
                AssetViewField OAssetViewField = new AssetViewField();
                OAssetViewField.Id= XmlUtility.GetString(ds.Tables["XmlInfo"].Rows[iIndex],"Id",true);
                OAssetViewField.Title = XmlUtility.GetString(ds.Tables["XmlInfo"].Rows[iIndex], "Title", true);
                OAssetViewField.ToolTips = XmlUtility.GetString(ds.Tables["XmlInfo"].Rows[iIndex], "ToolTips", true);
                OAssetViewField.SqlField = XmlUtility.GetString(ds.Tables["XmlInfo"].Rows[iIndex], "SqlField", true);
                OAssetViewField.LookUp = XmlUtility.GetInt(ds.Tables["XmlInfo"].Rows[iIndex], "LookUp", true);
                OAssetViewField.LookUpTable = XmlUtility.GetString(ds.Tables["XmlInfo"].Rows[iIndex], "LookUpTable", true);
                OAssetViewField.LookUpKeyId = XmlUtility.GetString(ds.Tables["XmlInfo"].Rows[iIndex], "LookUpKeyId", true);
                OAssetViewField.Key = XmlUtility.GetInt(ds.Tables["XmlInfo"].Rows[iIndex], "Key", true);
                OAssetViewField.FieldType = XmlUtility.GetString(ds.Tables["XmlInfo"].Rows[iIndex], "FieldType", true);
                OAssetViewField.SortIndex = XmlUtility.GetString(ds.Tables["XmlInfo"].Rows[iIndex], "SortIndex", true);
                OAssetViewField.Active = XmlUtility.GetInt(ds.Tables["XmlInfo"].Rows[iIndex], "Active", true);
               
                mAssetViewFields.Add(OAssetViewField);
            }

            return mAssetViewFields; 
        }


        public static ParamCls[] GetSearchParams(string SiteId, string AssetTypeId)
        {
            AssetTypeCls OAssetType = AssetBll.CreateAssetTypeModel(SiteId, AssetTypeId,false);
            DataSet ds = null;
            ParamCls[] Params = new ParamCls[0];
            if (!string.IsNullOrEmpty(OAssetType.XmlConfigSearch))
            {
                ds = new DataSet();
                ds.ReadXml(new StringReader(OAssetType.XmlConfigSearch));
                DataView dv = new DataView(ds.Tables[0], "active=1", "SortIndex", DataViewRowState.CurrentRows);
                Params = new ParamCls[dv.Count];
                for (int iIndex = 0; iIndex < dv.Count; iIndex++)
                {
                    string Key = dv[iIndex]["SQLField"].ToString();
                    string Value = "";
                    string Title = dv[iIndex]["Title"].ToString();
                    Params[iIndex]=new ParamCls(Key, Title, Value, true);
                }
            }
            return Params;
        }


        public static ParamCls[] GetUpdateParams(string SiteId, string AssetTypeId)
        {
            AssetTypeCls OAssetType = AssetBll.CreateAssetTypeModel(SiteId, AssetTypeId, false);
            DataSet ds = null;
            ParamCls[] Params = new ParamCls[0];
            if (!string.IsNullOrEmpty(OAssetType.XmlFieldConfig))
            {
                ds = new DataSet();
                ds.ReadXml(new StringReader(OAssetType.XmlFieldConfig));
                DataView dv = new DataView(ds.Tables[0], "Active=1", "SortIndex", DataViewRowState.CurrentRows);
                Params = new ParamCls[dv.Count];
                for (int iIndex = 0; iIndex < dv.Count; iIndex++)
                {
                    string Key = dv[iIndex]["SQLField"].ToString();
                    string Value = "";
                    string Title = dv[iIndex]["Title"].ToString();
                    string FieldType = dv[iIndex]["FieldType"].ToString();
                    Params[iIndex] = new ParamCls(Key, Title, Value, true);
                    Params[iIndex].Type = FieldType;
                }
            }
            return Params;
        }


        public static ParamCls[] GetPrintParams(string SiteId, string AssetTypeId)
        {
            AssetTypeCls OAssetType = AssetBll.CreateAssetTypeModel(SiteId, AssetTypeId, false);
            DataSet ds = null;
            ParamCls[] Params = new ParamCls[0];
            if (!string.IsNullOrEmpty(OAssetType.XmlConfigPrint))
            {
                ds = new DataSet();
                ds.ReadXml(new StringReader(OAssetType.XmlConfigPrint));
                DataView dv = new DataView(ds.Tables[0], "active=1", "SortIndex", DataViewRowState.CurrentRows);
                Params = new ParamCls[dv.Count];
                for (int iIndex = 0; iIndex < dv.Count; iIndex++)
                {
                    string Key = dv[iIndex]["SQLField"].ToString();
                    string Value = "";
                    string Title = dv[iIndex]["Title"].ToString();
                    Params[iIndex] = new ParamCls(Key, Title, Value, true);
                }
            }
            return Params;
        }
    }
}
